package com.msk.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * <p>[xiangyoukeji]</p>
 * <p>作者：xiangyou</p>
 * <p>邮箱：xiangyou@qq.com</p>
 * <p>创建时间： 2016-6-12 下午1:55:34 </p>
 * <p>类说明：数字格式化方法</p>
 * <p>修改记录： </p>
 */
public class NumberFormatUtils extends org.apache.commons.lang3.math.NumberUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(NumberFormatUtils.class);

	/**
	 * 格式化保留一位小数：四舍五入
	 * @param v
	 * @param scale
	 * @return double
	 * @throws
	 */
	public static double round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * 值转换
	 * @param value
	 * @param coefficienttype
	 * @param coefficient
	 * @return double
	 * @throws
	 */
	public static double doubleValueCompute(Double value, Double coefficient, String coefficienttype) {
		logger.info("value：" + value + "coefficient：" + coefficient + "coefficienttype：" + coefficienttype);
		Double computeValue = value;
		/**
		 * 1 2 3 4 代表加减乘除
		 */
		switch (Integer.parseInt(coefficienttype)) {
			case 1: {
				computeValue = value + coefficient;
				break;
			}
			case 2: {
				computeValue = value - coefficient;
				break;
			}
			case 3: {
				computeValue = value * coefficient;
				break;
			}
			case 4: {
				computeValue = value / coefficient;
				break;
			}
		}
		return computeValue;
	}

	/**
	 * 值转换
	 * @param value
	 * @param coefficienttype
	 * @param coefficient
	 * @return double
	 * @throws
	 */
	public static double doubleValueTranslate(Double value, Double coefficient, String coefficienttype) {
		logger.info("value：" + value + "coefficient：" + coefficient + "coefficienttype：" + coefficienttype);
		Double computeValue = 0.0;
		/**
		 * 1 2 3 4 代表加减乘除：此处需要反转运算
		 */
		switch (Integer.parseInt(coefficienttype)) {
			case 1: {
				computeValue = value - coefficient;
				break;
			}
			case 2: {
				computeValue = value + coefficient;
				break;
			}
			case 3: {
				computeValue = value / coefficient;
				break;
			}
			case 4: {
				computeValue = value * coefficient;
				break;
			}
		}
		return computeValue;
	}

	/**
	 * 格式化字符串保留指定位数
	 * @param v
	 * @param scale
	 * @return
	 */
	public static String roundByScale(String v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The   scale   must   be   a   positive   integer   or   zero");
		}
		if(scale == 0 || StringUtils.isBlank(v)){
			v = "0";
			return new DecimalFormat("0").format(Double.parseDouble(v));
		}
		String formatStr = "0.";
		for(int i=0;i<scale;i++){
			formatStr = formatStr + "0";
		}
		return new DecimalFormat(formatStr).format(Double.parseDouble(v));
	}

	public static String doubleToStr(Double d){
		String resut = "";
		Double r = 0.00;
		try{
			resut = Math.round(d) + "";
		}catch(Exception e){
			logger.error(e.toString());
		}
		return resut;
	}

	/**
	 * @auther addBy wlxiang
	 * @date 20201026
	 * @param divisor 除数
	 * @param divided 被除数
	 * @param keepCount 保留小数位
	 * @return
	 */
	public static String calculationDivide(BigDecimal divisor,BigDecimal divided,int keepCount){
		String result = "";
		try{
			result = divisor.divide(divided,keepCount,BigDecimal.ROUND_HALF_UP) + "";
		}catch(Exception e){
			logger.error(e.toString());
		}
		return result;
	}

	/**
	 * @auther addBy wlxiang
	 * @date 20201026
	 * @param multiplier 程数
	 * @param multiplied 被程数
	 * @param keepCount 保留小数位
	 * @return
	 */
	public static String calculationMultiply(BigDecimal multiplier,BigDecimal multiplied,int keepCount){
		String result = "";
		try{
			result = multiplier.multiply(multiplied).setScale(keepCount,BigDecimal.ROUND_HALF_UP) + "";
		}catch(Exception e){
			logger.error(e.toString());
		}
		return result;
	}


	/**

	 * 判断字符串是不是double型

	 * @param str

	 * @return

	 */

	public static boolean isNumeric(String str){
		if (null == str || "".equals(str)) {
			return false;
		}
		String regx = "[+-]*\\d+\\.?\\d*[Ee]*[+-]*\\d+";
		Pattern pattern = Pattern.compile(regx);
		boolean isNumber = pattern.matcher(str).matches();
		if (isNumber) {
			return isNumber;
		}
		regx = "^[-\\+]?[.\\d]*$";
		pattern = Pattern.compile(regx);
		return pattern.matcher(str).matches();
	}

	public static void main(String[] args) {
		//System.out.println(doubleToStr(12.46));
		BigDecimal b1 = new BigDecimal(2.02);
		BigDecimal b2 = new BigDecimal(2.02);
		System.out.println(isNumeric("1.012"));
		System.out.println(isNumeric("222.wqq"));
		System.out.println(isNumeric("qweqe"));
		System.out.println(isNumeric("1.3321"));

	}

}
